package com.skyz.wrap.utils;

import android.app.Dialog;
import android.content.Context;

import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.allenliu.versionchecklib.v2.builder.DownloadBuilder;
import com.allenliu.versionchecklib.v2.builder.UIData;
import com.allenliu.versionchecklib.v2.callback.CustomVersionDialogListener;
import com.allenliu.versionchecklib.v2.callback.ForceUpdateListener;
import com.skyz.wrap.dialog.AppUpdateDialog;
import com.skyz.wrap.entity.result.Version;

public class UpdateVersionUitl {

    private UpdateVersionUitl(){

    }
    private static UpdateVersionUitl instance = new UpdateVersionUitl();

    public static UpdateVersionUitl getInstance(){
        return  instance;
    }

    public void update(Context context, Version version){
        DownloadBuilder builder = AllenVersionChecker
                .getInstance()
                .downloadOnly(crateUIData("有新版本啦","更新版本体验更多功能!",version.getAndroidAddress()));
                if (version.getOpenUpgrade()){
                    builder.setForceUpdateListener(new ForceUpdateListener() {
                        @Override
                        public void onShouldForceUpdate() {
//                            Application application = BaseApplication.getApplication();
//                            UserInfoManager.getInstance().setUserInfo(application, null);
//                            UserInfoManager.getInstance().setUserInfoDetail(application, null);
//                            ActivityManager.getInstance().finishAllActivity();
//                            RouteManager.getInstance().showActivity(application, "WelcomeActivity");
                        }
                    });
                }
                builder.setShowNotification(true);
                builder.setShowDownloadingDialog(true);
                builder.setShowDownloadFailDialog(true);
                builder.setForceRedownload(false);
                builder.setCustomVersionDialogListener(new CustomVersionDialogListener() {
                    @Override
                    public Dialog getCustomVersionDialog(Context context, UIData versionBundle) {
                        AppUpdateDialog dialog = new AppUpdateDialog(context);
                        dialog.setUiData(versionBundle,version.getOpenUpgrade());
                        return dialog;
                    }
                });
             builder.executeMission(context);
    }


    private UIData crateUIData(String title,String hint,String apkurl) {
        UIData uiData = UIData.create();
        uiData.setTitle("时空驿站");
        uiData.setDownloadUrl(apkurl);
        uiData.setContent(hint);
        return uiData;
    }
}
